using System;
using System.Threading.Tasks;

namespace HelloWorld.Learn
{
    public class AsyncDemo
    {
        public async Task<int> GetRandIntAsync()
        {
            var r = new Random();
            var result = r.Next(100, 999);
            Console.WriteLine(result);

            await Task.Delay(100);

            return result;
        }

        public int GetRandInt()
        {
            //var result = GetRandIntAsync().GetAwaiter().GetResult();
            var result = GetRandIntAsync().Result;
            return result;
        }

        public async Task<double> GetRandDoubleAsync()
        {
            var result = await Task.Run(() => GetRandDouble());
            return result;
        }

        public double GetRandDouble()
        {
            var r = new Random();
            var result = r.NextDouble();
            Console.WriteLine(result);

            return result;
        }

        public static void Demo()
        {
            var demo = new AsyncDemo();
            // 异步转同步
            demo.GetRandDoubleAsync().Wait();
            // 异步转同步
            var result = demo.GetRandInt();
            //Console.WriteLine(result);
        }
    }
}
